//
//  KeHuViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/1/24.
//  Copyright © 2024 iningKe. All rights reserved.
//

#import "KeHuViewController.h"
#import "KeHuListViewController.h"
#import "GongHaiViewController.h"
#import "KeHuSouSuoViewController.h"
#import "KeHuChaChongViewController.h"

@interface KeHuViewController (){
    NSInteger currentIndex;
    UIButton *searchButton;
    UILabel *chaChongLabel;
    NSString *poolId;
    
    NSArray *titleArray;
}

@end

@implementation KeHuViewController


- (instancetype)init{
    if (self = [super init]) {
        [self huoQuGeRenYongHuQuanXian];
        
    }
    return self;
}

//获取个人权限
- (void)huoQuGeRenYongHuQuanXian{
    titleArray = @[@"客户"];
    [[ZZPRequest getWebUtil] postNetworkRequestURLString:@"/adminRole/auth" andParas:nil andShowHud:NO andTransferGottenData:^(id info, NSError *error) {
        if(!error){
            NSDictionary *data = [info objectForKey:@"data"];
            NSDictionary *crm = [data objectForKey:@"crm"];
    
            NSDictionary *customerall = [crm objectForKey:@"customerall"];
            NSDictionary *pool = [crm objectForKey:@"pool"];
            
            NSMutableArray *tempArray = [NSMutableArray array];
            [tempArray addObject:@"客户"];
            if(pool.allValues.count > 0){
                [tempArray addObject:@"公海"];
            }
            
            if(customerall.allValues.count > 0){
                [tempArray addObject:@"客户总览"];
            }
            titleArray = [NSArray arrayWithArray:tempArray];
        }
        [self loadCustomView];
    }];
}



- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    searchButton = [[UIButton alloc] init];
    [searchButton setImage:LocalImagePath(@"客户搜索") forState:UIControlStateNormal];
    [self.view addSubview:searchButton];
    searchButton.sd_layout
    .rightSpaceToView(self.view, 16)
    .topSpaceToView(self.view, StatusBarHeight+12)
    .heightIs(20)
    .widthIs(20);
    
    [searchButton addTarget:self action:@selector(searchClick) forControlEvents:UIControlEventTouchUpInside];
    
    
    chaChongLabel = [[UILabel alloc] init];
    [self.view addSubview:chaChongLabel];
    chaChongLabel.text = @"查重";
    chaChongLabel.textColor = rgba(40, 120, 255, 1);
    chaChongLabel.font = [UIFont systemFontOfSize:14];
    chaChongLabel.textAlignment = NSTextAlignmentRight;
    chaChongLabel.sd_layout
    .rightSpaceToView(searchButton, 16)
    .centerYEqualToView(searchButton)
    .heightIs(20)
    .widthIs(80);
    
    [chaChongLabel addTarget:self action:@selector(chaChongClick)];
    
}

//            搜索
- (void)searchClick{
    KeHuSouSuoViewController *keHu = [[KeHuSouSuoViewController alloc] init];
    keHu.hidesBottomBarWhenPushed = YES;
    if (currentIndex == 1) {
        keHu.isGongHai = YES;
    }
    keHu.type = [NSString stringWithFormat:@"%ld",currentIndex];
    keHu.poolId = poolId;
    [self.navigationController pushViewController:keHu animated:YES];
}
//查重
- (void)chaChongClick{
    if(currentIndex == 1){
        //批量领取
        [[NSNotificationCenter defaultCenter] postNotificationName:@"shiFouPiLiangQu" object:nil];
        return;
    }
    KeHuChaChongViewController *chaZhong = [[KeHuChaChongViewController alloc] init];
    chaZhong.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:chaZhong animated:YES];
}

- (void)loadCustomView{
    NSArray *data = titleArray;
    WMZPageParam *param = PageParam()
    .wTitleArrSet(data)
    .wMenuBgColorSet([UIColor whiteColor])
    .wMenuTitleColorSet(rgba(163, 168, 185, 1))
    .wMenuTitleSelectColorSet(rgba(24, 27, 37, 1))
    .wMenuAnimalTitleGradientSet(YES)
//    .wMenuIndicatorImageSet(@"下划线")
    .wMenuIndicatorColorSet(rgba(40, 120, 255, 1))
    .wMenuIndicatorWidthSet(17.5)
    .wMenuIndicatorHeightSet(4)
    .wMenuIndicatorRadioSet(2)
    .wMenuWidthSet(PageVCWidth - 16)
    .wMenuPositionSet(PageMenuPositionCenter)
    .wMenuFixShadowSet(NO)

//    .wMenuFixRightDataSet(@[@{WMZPageKeyName:@"查重",WMZPageKeySelectName:@"查重",WMZPageKeyTitleColor:rgba(40, 120, 255, 1),WMZPageKeyTitleSelectColor:rgba(40, 120, 255, 1)},@{WMZPageKeyImage:@"客户搜索"}])
    .wMenuHeightSet(44)
    .wMenuTitleUIFontSet([UIFont systemFontOfSize:16])
    .wMenuTitleSelectUIFontSet([UIFont systemFontOfSize:18 weight:UIFontWeightMedium])
    //控制器数组
    .wViewControllerSet(^UIViewController *(NSInteger index) {
       
        if(index == 1){
            GongHaiViewController*vc = GongHaiViewController.new;
            vc.selelctCurrentPoolBlock = ^(NSString * _Nonnull result) {
                poolId = result;
            };
            return vc;
        }
        
        KeHuListViewController*vc = KeHuListViewController.new;
        vc.type = [NSString stringWithFormat:@"%ld",index];
        return vc;
    })
    .wCustomTabbarYSet(^CGFloat(CGFloat nowY) {
        return nowY;
    })
    .wBouncesSet(YES)
    //控制器结束切换
    .wEventEndTransferControllerSet(^(UIViewController *oldVC, UIViewController *newVC, NSInteger oldIndex, NSInteger newIndex) {
       NSLog(@"结束切换 %ld %ld",oldIndex,newIndex);
        if(oldIndex == newIndex){
            return;
        }
        currentIndex = newIndex;
        if(newIndex == 1){
            chaChongLabel.text = @"批量领取";
        }else{
            chaChongLabel.text = @"查重";
            if(newIndex == 2){
                chaChongLabel.hidden = YES;
            }else{
                chaChongLabel.hidden = NO;
            }
        }
        
     })
    .wMenuHeadViewSet(^UIView *{
        UIView *back = [UIView new];
        back.frame = CGRectMake(0, 0, ScreenWidth, StatusBarHeight);
        back.backgroundColor = [UIColor whiteColor];
        return back;
    })
    //悬浮开启
    .wTopSuspensionSet(NO)
    //顶部可下拉
    .wBouncesSet(NO)
    //头视图y坐标从导航栏开始
    .wFromNaviSet(NO)
    //导航栏透明度变化
    .wMenuDefaultIndexSet(currentIndex)
    .wNaviAlphaSet(YES)
    .wMenuAnimalSet(PageTitleMenuAiQY);


    param.wCustomDataViewHeight = ^CGFloat(CGFloat nowY) {
        /// 再减掉父类的菜单高度
        return nowY-44-[self vg_tabBarFullHeight];
    };
    //右边固定标题点击
    param.wEventFixedClickSet(^(id anyID, NSInteger index) {
        NSLog(@"固定标题点击%ld",index);
        if(index == 10087){
//            搜索
            KeHuSouSuoViewController *keHu = [[KeHuSouSuoViewController alloc] init];
            keHu.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:keHu animated:YES];
        }else if (index == 10086){
            KeHuChaChongViewController *chaZhong = [[KeHuChaChongViewController alloc] init];
            chaZhong.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:chaZhong animated:YES];
        }
    });
    self.param = param;
}
/// 底部安全区高度
- (CGFloat)vg_safeDistanceBottom {
    if (@available(iOS 13.0, *)) {
        NSSet *set = [UIApplication sharedApplication].connectedScenes;
        UIWindowScene *windowScene = [set anyObject];
        UIWindow *window = windowScene.windows.firstObject;
        return window.safeAreaInsets.bottom;
    } else if (@available(iOS 11.0, *)) {
        UIWindow *window = [UIApplication sharedApplication].windows.firstObject;
        return window.safeAreaInsets.bottom;
    }
    return 0;
}



/// 底部导航栏高度
- (CGFloat)vg_tabBarHeight {
    return 49.0f;
}

/// 底部导航栏高度（包括安全区）
-(CGFloat)vg_tabBarFullHeight {
    return [self vg_tabBarHeight] + [self vg_safeDistanceBottom];
}
//
//  KeHuViewController.m
//  JuBoCRM
//
//  Created by iMac on 2024/1/24.
//  Copyright © 2024 iningKe. All rights reserved.
//
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end




